package com.example.Servlet;

import com.example.Function.DataBaseOp;
import com.example.Function.IdWorker;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * @author echo
 *建表
 */
@WebServlet("/CreatTable")
public class CreatTable extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        String user = req.getParameter("user");
        String id = String.valueOf(new IdWorker(1, 1, 1).nextId());
        //建表
        DataBaseOp.creatTable('a'+id);
        DataBaseOp.creatErrorBook('e'+id);
        //记录
        DataBaseOp.insertTableName(title,'a'+id,user);
        DataBaseOp.insertErrorBookName(title,'a'+id,user,'e'+id);
        //全局记录
        DataBaseOp.publicTile = 'a'+id;

        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        out.println(gson.toJson(true));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
